// /** 存储类，用于存储需要本地保存的数据 */

import { _decorator, sys } from 'cc';
interface YangLeGeYang_GameStorageData {
    current_level: number;
}

export class YangLeGeYang_GameStorage {
    static data: YangLeGeYang_GameStorageData = null;
    static defaultValue: YangLeGeYang_GameStorageData = null;
    static Game_Name: string = `YangLeGeYang`;
    static initData() {
        YangLeGeYang_GameStorage.InitDefaultData();

        let dataStr = sys.localStorage.getItem(`${YangLeGeYang_GameStorage.Game_Name}_GameStorage`);
        if (!dataStr || dataStr === "") {
            YangLeGeYang_GameStorage.clearData();
        } else {
            YangLeGeYang_GameStorage.data = JSON.parse(dataStr);
            YangLeGeYang_GameStorage.saveData();
        }

        YangLeGeYang_GameStorage.SetEveryData();
    }
    //    // 初始化默认值
    static InitDefaultData() {
        YangLeGeYang_GameStorage.defaultValue = {
            current_level: 0,
        };
    }
    static SetEveryData() {
        for (let key in YangLeGeYang_GameStorage.defaultValue) {
            let defaultValue = YangLeGeYang_GameStorage.defaultValue[key];
            let dataValue = YangLeGeYang_GameStorage.data[key];
            // console.log(key, defaultValue, dataValue);
            if (dataValue === null || dataValue === undefined) {
                YangLeGeYang_GameStorage.data[key] = defaultValue;
            }
        }
        YangLeGeYang_GameStorage.saveData();
    }
    static clearData() {
        YangLeGeYang_GameStorage.data = { ...YangLeGeYang_GameStorage.defaultValue };
    }
    static saveData() {
        let dataStr = JSON.stringify(YangLeGeYang_GameStorage.data);
        sys.localStorage.setItem(`${YangLeGeYang_GameStorage.Game_Name}_GameStorage`, dataStr);
    }
}